﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using University.Workshop.DataAccess;
using University.Workshop.Model;
using University.Workshop.Business;

namespace University.Workshop.WebApp
{
    public partial class ViewUserWishList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            guestUserIdLabel.Text = Request["GuestUserId"];
            User user = UserDataAccess.GetUserByEmail(User.Identity.Name);
            UserWishList userWL = new UserWishList();
            if(String.IsNullOrEmpty(guestUserIdLabel.Text))
                userWL = new UserWishList { UserId = user.Id };
            else
                userWL = new UserWishList { UserId = Convert.ToInt64(guestUserIdLabel.Text) }; 
            /*productIdLabel.Text = Request["ProductID"];
            userIdLabel.Text = Request["UserId"];
            UserWishList userWL = new UserWishList {  UserId = Convert.ToInt64(userIdLabel.Text),ProductId = Convert.ToInt64(productIdLabel.Text),Status = "Available"};*/
            GridView1.DataSource = UserWishListManager.LoadUserWishList(userWL);
            GridView1.DataBind();
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                GridViewRow row = GridView1.SelectedRow;
                LabelProdID.Text = row.Cells[1].Text;
                LabelProdNAME.Text = row.Cells[2].Text;
                LabelProdPrice.Text = row.Cells[4].Text;
                Response.Redirect("ProductSelected.aspx?ProductID=" + LabelProdID.Text + "&ProductName=" + LabelProdNAME.Text + "&ProductPrice=" + LabelProdPrice.Text + "&ProductCategory=" + row.Cells[3].Text);
            }
            catch
            {
                LabelMessage.Visible = true;
                LabelMessage.Text = "No rows Selected";
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Response.Redirect("FindWishList.aspx?");
        }
        protected void Delete_Click(object sender, EventArgs e)
        {
            LinkButton b = sender as LinkButton;
            User user = DataAccess.UserDataAccess.GetUserByEmail(User.Identity.Name);
            UserWishListDataAccess.DeleteProductWishList(Convert.ToInt64(b.CommandArgument));

            guestUserIdLabel.Text = Request["GuestUserId"];
            UserWishList userWL = new UserWishList();
            if (String.IsNullOrEmpty(guestUserIdLabel.Text))
                userWL = new UserWishList { UserId = user.Id };
            else
                userWL = new UserWishList { UserId = Convert.ToInt64(guestUserIdLabel.Text) }; 

            GridView1.DataSource = UserWishListManager.LoadUserWishList(userWL);
            GridView1.DataBind();
        }
    }
}